<?php
require 'include/global.php';
require './include/validate.php';
require './include/all_left.php';
include("FCKeditor/fckeditor.php") ;

require_once 'include/advisory_db.php';
$smarty->compile_check = true;

$smarty->display('header.tpl');
      
if($_GET[rid]){
	//applicant Info
	$sql = "SELECT * FROM pa_applicant WHERE app_id = '".$_GET[rid]."' AND app_ownid = '".$_SESSION['uid']."'";
	$applicantInfo = $DB->fetch_one_array($sql);
	$rid = $_GET[rid];
	
	//resume Info
	if($applicantInfo['app_resumeid'] >0){
		$sql = "SELECT re_fname, re_lname, re_nickname, re_email FROM pa_resume WHERE re_id = '".$applicantInfo['app_resumeid']."' ";
		$resumeInfo = $DB->fetch_one_array($sql);
	}

	//job Info
	$sql = "SELECT jo_id,jo_title FROM pa_job WHERE jo_id = '".$applicantInfo['app_jobid']."' ";
	$jobInfo = $DB->fetch_one_array($sql);

	//rec Info
	if($applicantInfo[app_recruitid] > 0){
		$sql = "SELECT rc_fname, rc_lname FROM pa_recuiter WHERE rc_id='".$applicantInfo['app_recruitid']."'";
		$recInfo = $DB->fetch_one_array($sql);
	}
	
	//email ID check
	if($_GET['emailId']>0){
		$emailId = $_GET['emailId'] + 0;
		$sql = "SELECT * FROM pa_email WHERE e_id = '".$emailId."' AND e_receivedrcid='".$_SESSION['uid']."' ";
		$emailInfo = $DB->fetch_one_array($sql);
		$emailUrl = "&emailId=".$emailId;
		if(empty($emailInfo)){
			//go to not authorized to view this page . line 48
			$applicantInfo = array();
		}
	}
}
if(empty($applicantInfo)){
	$smarty->compile_check = true;

   $err = "You are not authorized to view this page.";
   $smarty->assign("err",$err);
   $smarty->display("error.tpl");
   $smarty->display("allright.tpl");
   $smarty->display('footer.tpl');
   exit;
}


if($_POST['send'] == 1){
	
	if(trim($_POST['rc_subject']) == ''){
		$err .= "Subject is required<br>";
	}
	if (trim(strip_tags($_POST['rc_detail'])) == '')
	{
		$err .= "Message is required<br>";
	}
	if(	$err == ''){
			$receivedrcId = '';
			if($applicantInfo['app_owntype'] == 'candidate'){
				//candidate
				$to_email = $resumeInfo['re_email'];
				$to_name = $resumeInfo['re_fname'] ." ". $resumeInfo['re_lname'];
				$receivertype = 'candidate';
				$etype = "2";
			}elseif($applicantInfo['app_owntype'] == 'recruiter' && $applicantInfo['app_recruitid'] > 0 ){
				$applicantInfo['app_recruitid'];
				$sql = "SELECT rc_fname,rc_lname,rc_copname,rc_email FROM pa_recuiter WHERE rc_id = '".$applicantInfo['app_recruitid']."'";
				$rcInfo = $DB->fetch_one_array($sql);
				//recruiter
				$to_email = $rcInfo['rc_email'];
				$to_name = $rcInfo['rc_fname']." ".$rcInfo['rc_lname'];
				$receivedrcId = $applicantInfo['app_recruitid'];
				$receivertype = 'recruiter';
				$etype = "1";
			}

			//Insert to Email table
			$sql = "INSERT INTO pa_email (e_sentrcid, e_receivedrcid,e_sendertype,e_receivertype, e_joid, e_type, e_fromemail, e_fromname, e_toemail, e_toname, e_subject, e_content, e_created) VALUES (
				'".$_SESSION['uid']."',
				'".$receivedrcId."',
				'recruiter',
				'".$receivertype."',
				'".$jobInfo['jo_id']."',
				'".$etype."',
				'".$_SESSION['useremail']."',
				'".$_SESSION['rc_copname']." (".$_SESSION['realname'].")',
				'".$to_email."',
				'".$to_name."',
				'".$_POST['rc_subject']."',
				'".$_POST['rc_detail']."',
				'".time()."'
			)";
			$DB->query($sql);

			//Update the application
			if($rid > 0){
				$sql = " UPDATE pa_applicant SET app_isreplied = 1 WHERE app_id = '".$rid."' ";
				$DB->query($sql);
			}

			//External Email
			$content    = $_POST['rc_detail'];
			$subject    = $_POST['rc_subject'];

			require_once("./include/class.phpmailer.php");

			$mail=new PHPMailer();

			$mail->IsMail();

			$mail->From     = $_SESSION['useremail'];
			$mail->FromName = $_SESSION['realname'];
			$mail->AddAddress($to_email,$to_name);

			$mail->IsHTML(true);
			$mail->Subject = stripslashes($subject);
			$mail->Body	= stripslashes($content);
			$mail->CharSet = 'UTF-8';
			$mail->Send();
			if($_GET[emailId]>0){
				$err = "<h3 style=\"color:red\">Sent Successfully!</h3><meta http-equiv=\"refresh\" content=\"3;url=myemails.php?op_folder=1\">";
			}else{
				$err = "<h3 style=\"color:red\">Sent Successfully!</h3><meta http-equiv=\"refresh\" content=\"3;url=applicantlist.php?&id=".$jobInfo['jo_id']."&tpp=posted\">";
			}
		}
}

ob_clean();
ob_start();

$datenow = date("m-d-Y");
$to  = ($applicantInfo['app_owntype'] == 'candidate')?$resumeInfo['re_fname'] ." ". $resumeInfo['re_lname']:$recInfo['rc_fname'].' '.$recInfo['rc_lname']." (Recruiter)";
$subject = "RE: Your Application for ". $jobInfo['jo_title'];

//FCKeditor
$oFCKeditor = new FCKeditor("rc_detail") ;
$oFCKeditor->BasePath	= "FCKeditor/" ;
$oFCKeditor->Value = '' ;
$oFCKeditor->Width = 450;
$oFCKeditor->Height= 300;
$oFCKeditor->ToolbarSet = "Basic";
$oFCKeditor->Value = "<br><br><br>-----------------------------<br>On ".date("m/d/Y",$applicantInfo['app_regtime']).", <b>".$to."</b> submitted <b>". $resumeInfo['re_nickname']."</b> for the position:<br><b>". $jobInfo['jo_title']."</b> (Job Req #".$jobInfo['jo_id'].") <br>" . "<b>Name: </b>".$resumeInfo['re_fname'] ." ". $resumeInfo['re_lname']."<br><b>Email: </b> ".$resumeInfo['re_email']."<br><b>Phone: </b> ".$resumeInfo['re_phone1']."<br>";

$oFCKeditor->Value .= "<b>Name: </b>".$resumeInfo['re_fname'] ." ". $resumeInfo['re_lname']."<br><b>Applied to Job: </b> ".$jobInfo['jo_title']."<br><b>ER Job Req #: </b> ".$jobInfo['jo_id']."<br><br><b>Resume Details: </b> <br><br>".$resumeInfo['re_detail'];


echo '
<table width="100%" align="center" border="0" cellspacing="0" cellpadding="0" class="table_style" >
<tr><td>
<form action="applicationreply.php?rid='.$rid.$emailUrl.'" method="POST" >
<table width="100%" border="0" align="center" cellpadding="4" cellspacing="1" >
<tr class="firstalt" nowrap><td width="30%"  style="font-weight:bold">
					Date:</td>
                       <td  width="70%" >'.$datenow.'</td>
                   </tr>
<tr class="firstalt" nowrap><td width="30%"  style="font-weight:bold">
					To :</td>
                       <td  width="70%" >'.$to.'</td>
                   </tr>
<tr class="firstalt" nowrap><td width="30%"  style="font-weight:bold">
					From :</td>
                       <td  width="70%" >'.$_SESSION['realname']."[".$_SESSION['useremail']."]".'</td>
                   </tr>
<tr class="firstalt" nowrap><td width="30%"  style="font-weight:bold">
					Subject:</td>
                       <td  width="70%" ><input name="rc_subject" value="'.$subject.'" style="width:400px;backgroud-color:#FFFFFF;border:1px solid #999999;height:20px"></td>
                   </tr>
<tr class="firstalt" nowrap><td width="30%"  style="font-weight:bold">
				              Message:  </td><td width="70%" >'.$oFCKeditor->CreateHtml().'
							 </td>
					   </tr>
<tr>
		<td  align="center" colspan="2">
		<input type="hidden" name="send" value="1" />
		<input class="bginput button-secondary" type="submit"  value=" Send Message " />
		<input class="bginput button-secondary" type="button"  value=" Cancel " onclick="location.href=\'applicantlist.php?&id='.$applicantInfo['app_jobid'].'&tpp=posted\'"/>
		</td>
		</tr>
		</table>
</from></td></tr></table>
		';

$reg_data = ob_get_contents();
ob_end_clean();

if ($err != '')
{
	$smarty->assign("err", $err);
}
$smarty->assign("reg", $reg_data);

if($applicantInfo['app_owntype'] == 'candidate'){
	$title   = "Reply to Applicant";
}elseif($applicantInfo['app_owntype'] == 'recruiter' && $applicantInfo['app_recruitid'] > 0 ){
	$title   = "Reply to Recruiter";
}

$smarty->assign('title',$title);
$smarty->display('allleft.tpl');

$smarty->display('contactreg.tpl');


$smarty->display('allright.tpl');
$smarty->display('footer.tpl');
?>
